package com.leetcode.sorts.string;

/**
 * @Author: WuPeng
 * @Date: 2021/5/27 8:06 下午
 *
 * 剑指Offer 05.替换空格
 *
 *
 */
public class ReplaceBlank {
    public static String solution(String s){
        int len = s.length();
        int blank_count = 0;
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) == ' '){
                blank_count++;
            }
        }
        int new_len = len + blank_count * 2;
        char[] chars = new char[new_len];
        int index = 0;
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) == ' '){
                chars[index++] = '%';
                chars[index++] = '2';
                chars[index++] = '0';
            }
            else {
                chars[index++] = s.charAt(i);
            }
        }
        return new String(chars);
    }

    public static void main(String[] args) {
        String str = "Hello world.";
        System.out.println(solution(str));
    }

}
